﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Deployment.Application;
using System.Web;
using System.Collections.Specialized;
using System.Configuration;

namespace FilesUpload
{
    public class helper
    {
        public static string GetTemplateFileHandlerPath()
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress += @"/Views/DataManage/PLuploads/TemplateFileHandler.ashx";
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static string GetArchiveNumRepeatQueryHandlerPath()
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress += @"/Views/DataManage/PLuploads/ArchiveNumRepeatQueryHandler.ashx";
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static string GetArchiveExportHandlerPath()
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress += @"/Views/DataManage/PLuploads/ArchiveExportHandler.ashx";
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static string GetFileUploadHandlerPath()
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress += @"/Views/DataManage/PLuploads/FileUploadHandler.ashx";
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static string GetGetCodeTableHandlerPath()
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress += @"/Views/DataManage/PLuploads/GetCodeTableHandler.ashx";
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static string GetUploadTemplatePath(string type, string catalogId)
        {
            NameValueCollection nameValueTable = GetQueryStringParameters();
            string webAddress = nameValueTable.Get("WebAddress");
            if (string.IsNullOrEmpty(webAddress))
            {
                webAddress = ConfigurationManager.AppSettings["WebAddress"];
            }
            else
            {
                webAddress = HttpUtility.UrlDecode(webAddress);
            }
            if (webAddress != null && !string.IsNullOrEmpty(webAddress))
            {
                if (webAddress.EndsWith("/"))
                {
                    webAddress = webAddress.Remove(webAddress.Length - 1);
                }
                webAddress = webAddress + @"/Views/DataManage/PLuploads/UploadTemplate/";
                switch (type)
                {
                    case "SingleWell":
                        webAddress += "单井批量模板.xls";
                        break;
                    case "Archive":
                        switch (catalogId)
                        {
                            case "1":
                                webAddress += "文书.xls";
                                break;
                            case "2":
                                webAddress += "光盘.xls";
                                break;
                            case "3":
                                webAddress += "科研.xls";
                                break;
                            case "4":
                                webAddress += "基建.xls";
                                break;
                            case "5":
                                webAddress += "设备.xls";
                                break;
                            case "61":
                                webAddress += "会计凭证.xls";
                                break;
                            case "62":
                                webAddress += "会计账簿.xls";
                                break;
                            case "63":
                                webAddress += "财务报告.xls";
                                break;
                            case "64":
                                webAddress += "会计其他.xls";
                                break;
                            case "71":
                                webAddress += "勘探（综合）.xls";
                                break;
                            case "72":
                                webAddress += "勘探（单井）.xls";
                                break;
                            case "81":
                                webAddress += "声像视频.xls";
                                break;
                            case "82":
                                webAddress += "声像音频.xls";
                                break;
                            case "83":
                                webAddress += "声像图像.xls";
                                break;
                            case "91":
                                webAddress += "荣誉.xls";
                                break;
                            case "101":
                                webAddress += "文书卷.xls";
                                break;
                            case "102":
                                webAddress += "文书卷内件.xls";
                                break;
                            case "7101":
                                webAddress += "综合卷.xls";
                                break;
                            case "7102":
                                webAddress += "综合卷内件.xls";
                                break;
                            default:
                                throw new Exception("该档案类型模板不存在！");
                        }
                        break;
                    default:
                        throw new Exception("模板类型设置错误！");
                }
            }
            else
            {
                throw new Exception("网站地址未设置！");
            }
            return webAddress;
        }

        public static NameValueCollection GetQueryStringParameters()
        {
            NameValueCollection nameValueTable = new NameValueCollection();

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
                nameValueTable = HttpUtility.ParseQueryString(queryString);
            }

            return (nameValueTable);
        }

        public static string GetTemplateName(string TemplatePath)
        {
            if (!string.IsNullOrEmpty(TemplatePath))
            {
                string[] temp = TemplatePath.Split('/');
                if (temp.Length > 1)
                {
                    return temp[temp.Length - 1];
                }
            }
            return "";
        }

    }
}
